from django.shortcuts import render
from django.contrib.auth import logout
from django.http import  HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
from Online.models import  userinfo
def user_login(request):
    ret = {'status':''}
    if request.method == 'POST':
        print("POST")
        username = request.POST.get('username',None)
        print(username)

        password = request.POST.get('password',None)
        print(password)

        is_auth = all([username,password])
        print(is_auth)
        if is_auth:
            count = userinfo.objects.filter(user=username,
                                    password=password).count()
            if count == 1:
                return redirect('/index/')
            else:
                ret['status'] = '用户名或密码错误'
                print(ret['status'])
        else:
            ret['status':] = '用户名或密码不能为空'
            print(ret['status'])

    return render(request,"login.html",{'ret':ret})


def user_logout(request):
    logout(request)
    return HttpResponseRedirect(reverse('login'))
# Create your views here.
